home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / cc02.zip / CHOP.C < prev    next >
Text File  |  1985-07-22  |  10KB  |  269 lines

  1. /* This program splits the input file into smaller files. 
  2.  
  3.    USAGE: CHOP FILENAME SIZE -SWITCHES
  4.    Version 1.1.  Written by W. J. Kennamer and released into the public domain.
  5.      FILENAME is any valid MS-DOS filename.  Wildcards are not supported.
  6.      Output file names will be FILENAME.1, FILENAME.2, etc.
  7.      Output files are always terminated with a carriage return and a <ctrl>Z.
  8.      SIZE is the desired file size for each new file.
  9.      SWITCHES must follow both the FILENAME and SIZE parameters,
  10.        and may be entered in any order.  Switches may be combined.
  11.      Valid SWITCHES are:
  12.        -s  strict    ║ chop file at exact SIZE boundary.
  13.        -r  return    ║ chop file at first carriage return
  14.                      ║   following SIZE characters (default).
  15.        -px partition ║ partition file into x equal units.
  16.                          SIZE is ignored (and may be omitted) if
  17.                          you choose this option.
  18.  
  19. File pointer fp1 is the input file.  fp2 is always the output 
  20.    file, though its name changes as new output files are
  21.    opened.
  22.  
  23. */   
  24.  
  25. #include "stdio.h"
  26. #include "ctype.h"
  27.  
  28. #define VERSION "1.2"
  29. #define PROGNAME "CHOP"
  30. #define CTRL_Z   0x1a
  31. #define VOID     int
  32. #define CR       0x0d
  33. #define OFF   0
  34. #define ON   1
  35.  
  36. main(argc,argv)
  37. int argc;
  38. char *argv[];
  39.    {
  40.     VOID help();
  41.     VOID close_out();
  42.  
  43.    int c;                             /* current character                */
  44.    int num_output_files = 0;          /* no of files successfully created */
  45.    int count;                         /* no. of digits in size            */
  46.    int length;                        /* length of matching string        */
  47.    int normal = ON;                   /* normal CR breaks                 */
  48.    int strict = OFF;                  /* break exactly at boundary        */
  49.    int partition = OFF;               /* strict or normal                 */
  50.    int num_part;                      /* number of partitions             */
  51.    int int_size;                      /* integer for file size            */
  52.  
  53.    long i;                            /* counts bytes in output file      */
  54.    long size = 0;                     /* partition value -- file size     */
  55.    long byte_count = 0;               /* total bytes in original file     */
  56.    long inp_file_size = 0;            /* total bytes in original file     */
  57.    long out_file_size = 0;            /* total bytes in output file       */
  58.  
  59.    FILE *fp1,*fp2;                    /* fp1=input, fp2=output            */
  60.  
  61.    char *p;                           /* pointer to switch string         */
  62.    char *period_p;                    /* location of period in filename   */
  63.    char *output_file;                 /* output file name                 */
  64.    char filename[15];                 /* original file name               */ 
  65.    char *stpchr();                    /* substring match function (MS)    */
  66.    char line1[15];                    /* data buffer                      */
  67.    char line2[15];                    /* data buffer                      */
  68.    char *switches;                    /* string to hold switches          */
  69.  
  70.    if( argc > 4 || argc < 2 || *argv[3] == "-?" )
  71.       {
  72.       help();
  73.       exit(1);
  74.       }
  75.  
  76.    /* see which argument is the switch */
  77.    if( (length = stcpma(argv[2],"-")) != 0)
  78.       {
  79.       switches = line2;
  80.       strcpy(switches,argv[2]);
  81.       }
  82.    else if( (length = stcpma(argv[3],"-")) != 0)
  83.       {
  84.       switches = line2;
  85.       strcpy(switches,argv[3]);
  86.       }
  87.  
  88.    for( p = switches ; (p - switches) < strlen(switches)  ; p++ )
  89.       {
  90.       if( *p == 's' )
  91.          {
  92.          strict = ON;
  93.          normal = OFF;
  94.          }
  95.       else if ( *p == 'r' || *p == 'n')
  96.          {
  97.          normal = ON;  
  98.          strict = OFF;
  99.          }
  100.       else if ( *p == 'p')
  101.          {
  102.          partition = ON;  
  103.          if(stcd_i((p+1),&num_part) == 0)
  104.             {
  105.             printf("Invalid number of partitions.\n");
  106.             exit(1);
  107.             }
  108.          }
  109.       }
  110.  
  111.    if( (fp1 = fopen(argv[1],"r") ) == NULL)
  112.       {
  113.       printf("Cannot open %s for input.\n",argv[1]);
  114.       exit(1);
  115.       }
  116.  
  117.    /* determine file size */
  118.    if(partition == ON)
  119.       {
  120.  
  121.       if( fseek(fp1,0L,2) == -1)            /* position at EOF */
  122.          {
  123.          printf("Error seeking end of input file.\n");
  124.          exit(1);
  125.          }
  126.       
  127.       inp_file_size = ftell(fp1);         /* tell EOF  */
  128.  
  129.         /* each partition requires 3 extra bytes -- CTRL_Z, newline */
  130.       size = ((inp_file_size + 3*(long)num_part) / (long)num_part ) + 1 ;
  131.  
  132.       printf("  Input file size = %ld bytes\n",inp_file_size);
  133.       printf("   Partition size = %ld bytes\n",size);
  134.       printf("No. of partitions = %ld\n\n",(long)num_part);
  135.  
  136.       rewind(fp1);                     /* back to beginning of file */      
  137.       }
  138.    else                                 /* partition is not ON */
  139.       {                                 /* read SIZE from command line */
  140.  
  141.       count = stcd_i(argv[2],&int_size);  /* convert string to integer */
  142.       if(count == 0)
  143.          {
  144.          printf("Invalid size\n");
  145.          exit(1);
  146.          }
  147.       size = (long)int_size;         /* convert to long */
  148.       }
  149.  
  150.    strcpy(&filename,argv[1]);
  151.  
  152.    period_p = stpchr(&filename,'.');         /* locate the period, if any */
  153.  
  154.    if( period_p != NULL )
  155.       *period_p = '\0';                     /* chop off the period  */
  156.  
  157.    while ( (c = getc(fp1)) != EOF)   
  158.       {
  159.       ungetc(c,fp1);      
  160.  
  161.       (num_output_files)++;                 
  162.       if(num_output_files > 127)
  163.          {
  164.          printf("Too many files--%d.\nExiting program\n.",num_output_files);
  165.          exit(1);
  166.          }
  167.  
  168.       output_file = line1;                /* initialize output_file pointer */
  169.  
  170.       /* create the next filename */
  171.       sprintf(output_file,"%s.%-d",filename,num_output_files);
  172.  
  173.       if( (fp2 = fopen(output_file,"w")) == NULL)
  174.          {
  175.          printf("Cannot open %s for output\n",output_file);
  176.          printf("Exiting the program.\n");
  177.          printf("%d files created.\n",(num_output_files) - 1 );
  178.          printf("Actual original filesize = %u bytes.\n",byte_count);
  179.          exit(1);
  180.          }
  181.  
  182.       for( i = 0 ; i < (size - 3) ; i++ )      /* output until size reached */
  183.          {                                    /* leave 3 spaces for newline, CTRL Z */
  184.          c = getc(fp1);
  185.  
  186.          if( c == '\n')
  187.             {
  188.             byte_count += 2;                  /* newline counts as 2 bytes */
  189.             i++;
  190.             }
  191.          else
  192.             byte_count++;
  193.  
  194.          if( c == EOF )
  195.             {
  196.             close_out(fp2,output_file);
  197.             exit(0);
  198.             }
  199.          putc(c,fp2);
  200.          }
  201.  
  202.       if( normal == ON )
  203.          {
  204.          while( (c = getc(fp1)) != '\n' )   
  205.             {
  206.             if( c == EOF )
  207.                {
  208.                close_out(fp2,output_file);
  209.                exit(0);
  210.                }
  211.             putc(c,fp2);
  212.             byte_count++;
  213.             }
  214.          byte_count += 2;            /* count the newline (2 bytes) */
  215.          }   
  216.  
  217.       close_out(fp2,output_file);
  218.       }                           /* end while not eof */
  219.    }                              /* end of main()   */
  220.  
  221. /***************************************************************************/
  222. VOID help()
  223.    {
  224.    printf("Version %s -- 02/26/85\n",VERSION);
  225.    printf("USAGE: %s FILENAME SIZE -SWITCHES\n\n",PROGNAME); 
  226.     printf("Written by W. J. Kennamer (74025,514) and released into the public domain.\n");
  227.    printf("  FILENAME is any valid MS-DOS filename.  Wildcards are not supported.\n");
  228.    printf("  Output file names will be FILENAME.1, FILENAME.2, etc.\n");
  229.    printf("  Output files are always terminated with a carriage return and a <ctrl>Z.\n");
  230.    printf("  SIZE is the desired file size for each new file.\n");
  231.    printf("  SWITCHES must follow both the FILENAME and SIZE parameters,\n");
  232.    printf("    and may be ente